
public class Converter {

	String command;
	static int total_equation;
	int sum = 0;
	String expression;
	 Base3 mybase3;
	int []number ;
	int answer;
	boolean solution_found = false;

		Converter(int[] integer_input, int answer_input)
		{
			 number = integer_input;
			 sum = number[0];
			 mybase3 = new Base3(number.length);
			 answer = answer_input;
			 total_equation++;
		}


		
	void findPossibleEquation()
	{	
		int total_run = 0;
		
		do
		{
		command = mybase3.getNextCombination();
		expression = Integer.toString(sum);
		checkNextExpression();
		
		
		if (answer == sum)
		{
			solution_found = true;
			
			displayExpression();	
		}
		
		 sum = number[0];
		 
		total_run++;
		
		
		}
		
		while((total_run <mybase3.total_combination) && (solution_found==false) );
		
		if (solution_found==false)
		{
			System.out.println("Equation #"+total_equation+" :");
			System.out.println("Impossible.");
			System.out.println();
		}
		
		
	}
	
	void listAllExpression()
	{
		solution_found = false;
		mybase3.resetCommand();
		
		{	
			int total_run = 0;
			
			do
			{
			command = mybase3.getNextCombination();
			expression = Integer.toString(sum);
			checkNextExpression();
			
			
		
			displayExpression();
			sum = number[0];
			 
			total_run++;
			
			
			}
			
			while((total_run <mybase3.total_combination) );
			
			
				
			
		}
	}
	
	void checkNextExpression()
	{
		 
		 
		for(int k  = 1; k<=command.length();k++)
		{
			
			
		
			if (command.charAt(k -1) =='0')
			{
				
				sum = sum + number[k];
				expression +=   "+"  +Integer.toString(number[k])  ;
			
				
				
			}
			
			else if (command.charAt(k -1) =='1')
			{
				
				sum = sum  - + number[k];

				expression +=   "-"  +Integer.toString(number[k])  ;
			}
			
			else if (command.charAt(k -1) =='2')
			{
			
				sum = sum * number[k];

				expression +=   "*"  +Integer.toString(number[k])  ;
			}
			
			else 
			{
				System.out.println("got error :(");
				
			}
			
			
		}

		
	}
	void displayExpression()
	{
		
		if (solution_found ==false)
		{
			System.out.println("Expression is "+expression);
	        System.out.println("Sum is "+sum);
	        System.out.println("answer is "+answer);
	        System.out.println();
		}
		else
		{
			System.out.println("Equation #"+total_equation+" :");
			System.out.println(answer+" = "+expression);
			System.out.println();
		}
		
	
	}
	void callNextCommand()
	{
		
	}
	
	int  getTotalCombination()
	{
		return mybase3.getTotalCombination();
	}
	
}


